package com.project.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.blog.common.Result;
import com.project.blog.dto.UserQuery;
import com.project.blog.dto.UsermeQuery;
import com.project.blog.entity.User;
import com.project.blog.entity.Userme;
import com.project.blog.serviceImpl.UsermeService;
import com.project.blog.vo.UsermeVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(tags="用户地址信息模块")
@RestController
@ResponseBody
@RequestMapping("/userme")
public class UsermeController extends BaseController  {

    @Autowired
    private UsermeService usermeService;

    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @ApiOperation(value = "查询所有用户地址信息", notes="查询所有用户地址的接口")
    @GetMapping("/ListUser")
    @ResponseBody
    public List<Userme> ListUserme() {
        return usermeService.ListUserme();
    }

    /**
     * 分页
     * @return
     */
    @ApiOperation(value = "查询用户地址信息(带分页)", notes="查询用户地址信息(带分页)的接口")
    @GetMapping(value="/page")
    @ResponseBody
    public Result<?> page(Integer page) {
        int pageNow = page == null ? 1 : page;
        int pageSize = 5;
        int startRows = pageSize*(pageNow-1);
        List<Userme> list = usermeService.queryPage(startRows);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("records",list);
        map.put("total",pageSize);
        return Result.success(map);
    }

    /**
     * rows
     * 总数
     * @return
     */
    @ApiOperation(value = "总数", notes="查询总数的接口")
    @GetMapping(value="/rows")
    @ResponseBody
    public int rows(){
        return usermeService.getRowCount();
    }

    /**
     * ListByName
     * 通过名字查询单个用户
     * @return
     */
    @ApiOperation(value = "通过输入用户名查询", notes="通过输入用户名查询的接口")
    @GetMapping("/ListByName")
    @ResponseBody
    public Result<?> ListUsermeByName(String usermeName) {
        List<Userme> list = usermeService.findUsermeByName(usermeName);
        return Result.success(list);
    }

    /**
     * insert
     * 添加用户
     * @return
     */
    @ApiOperation(value = "添加单个用户", notes="添加单个用户的接口")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Result<?> insertUserme(Userme userme) {
        return Result.success(usermeService.insertUserme(userme));
    }


    /**
     * update
     * 修改用户
     * @return
     */
    @ApiOperation(value = "修改用户", notes="修改用户的接口")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> Update(Userme userme) {
        int result =usermeService.Update(userme);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("records",userme);
        if (result >= 1) {
            return Result.success(map);
        } else {
            return Result.error();
        }
    }

    /**
     * delete
     * 删除用户
     * @return
     */
    @ApiOperation(value = "删除用户", notes="删除用户的接口")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
        public Result<?> delete(Integer usermeId) {
        System.out.println(usermeId);
        usermeService.delete(usermeId);
        return Result.success();
    }

    /**
     * delete
     * 批量删除用户
     * 使用postman传参方式如下
     * 1.选择Body.
     * 2.再选择raw,点击右侧选择JSON格式
     * 3.在下方空白区域输入如下内容 [668,669]
     * 注意：668和669就是传入的id，根据该ids数组中的id来删除对应的用户
     * @return
     */
    //批量删除
    @ApiOperation(value = "批量删除用户", notes="批量删除用户的接口")
    @RequestMapping(value = "/dels", method = RequestMethod.POST)
        public Result<?> dels(@RequestBody List<Integer> ids){
        //删除
        if (!ids.isEmpty()){
            usermeService.dels(ids);
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }


    /**
     * add
     * 批量添加用户
     * 使用postman传参方式如下
     * 1.选择Body.
     * 2.再选择raw,点击右侧选择JSON格式
     * 3.在下方空白区域输入如下内容格式
     * [
     *      {
     *         "createTime": "2019-07-06T03:55:04.971Z",
     *         "deptName": "设计",
     *         "descr": "设计",
     *         "id": 0
     *     },
     *     {
     *         "createTime": "2019-07-06T03:55:04.971Z",
     *         "deptName": "运维",
     *         "descr": "运维",
     *         "id": 0
     *     },
     *     {
     *         "createTime": "2019-07-06T03:55:04.971Z",
     *         "deptName": "售前",
     *         "descr": "售前",
     *         "id": 0
     *     }
     * ]
     * 注意：
     * @return
     */
    //批量添加
    @ApiOperation(value = "批量添加用户", notes="批量添加用户的接口")
    @RequestMapping(value = "/insertDepartments", method = RequestMethod.POST)
    public Result<?> newDepartment(@RequestBody List<Userme> usermes ) {
        int result = usermeService.insertDepts(usermes);
        if(result > 0) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    /**
     * search
     * 多表查询（1对1查询）
     * 使用postman传参方式如下
     * 1.选择Params
     * 2.再选择key
     * 3.在key里面输入usermeId，在value中输入18
     * @return
     */
    @ApiOperation(value = "多表查询（1对1）", notes="多表查询（1对1）的接口")
    @RequestMapping(value = "/UsermeList", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> usermeList(int usermeId){
        List<UsermeVo> usermeAndIdcard = usermeService.findUsermeAndIdcard(usermeId);
        if(!usermeAndIdcard.isEmpty()) {
            return Result.success(usermeAndIdcard);
        } else {
            return Result.error();
        }
    }

}
















